package com.batdream.message.netty;

import io.netty.channel.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Wang Chen Peng
 * @date 2019/01/02
 * describe:
 */
@Component
@Log4j2
public class ChannelUserContext {

    private static ConcurrentHashMap<String, String> context;

    static {
        context = new ConcurrentHashMap<>();
    }

    String getUserId(String channelId) {
        return context.get(channelId);
    }

    void addUserId(String channelId, String userId) {
        log.debug("ChannelUserContext容器添加 channelId : {} , userId : {}", channelId,userId);
        context.put(channelId, userId);
    }

    boolean containsKey(String channelId) {
        return context.containsKey(channelId);
    }

    void outPut() {
        context.forEach((key, value) -> {
            System.out.println(" ConcurrentHashMap:key : " + key + "   ");
            System.out.println(" ConcurrentHashMap:value : " + value);
        });
    }

    void remove(String channelId){
        if (containsKey(channelId)){
            log.debug("ChannelUserContext容器移除 channelId : {} ", channelId);
            context.remove(channelId);
        }
    }
}
